<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
<!--    通过对类的工厂抽象，使其业务用于产品类簇的创建，而不负责创建某一类产品的实例-->

//抽象工厂方法
let VehicleFactory = function (subType,superType) {
  if (typeof VehicleFactory[superType] === 'function') {
    //缓存类
    function F () {}
    //继承父类属性和方法
    F.prototype = new VehicleFactory[superType]();
    //将子类constructor 指向子类
    subType.constructor = subType;
    //子类原型继承父类
    subType.prototype = new F();
  } else {
    throw new Error('未创建该抽象类');
  }
}
  //  小汽车抽象类
  VehicleFactory.Car = function (){
    this.type = 'car'
  }
  VehicleFactory.Car.prototype = {
    getPrice: function (){
      return new Error('抽象方法不能调用')
    },
    getSpeed: function (){
      return new Error('抽象方法不能调用')
    }
  }

//  宝马汽车子类
  let BMW = function (price,speed){
    this.price = price;
    this.speed = speed;
  }
//  抽象工厂实现对Car抽象类的继承
  VehicleFactory(BMW,'Car')
  //重写抽象父类的函数
  BMW.prototype.getPrice = function (){
    return this.price
  }
  BMW.prototype.getSpeed = function (){
    return this.speed
  }

  let myCar = new BMW(100000,23333);
  console.log(myCar.getPrice());
  console.log(myCar.getSpeed());

</script>
</body>
</html>
